// trevahn.txt
// The script for Trevahn, the final boss in Siege of Copperpeak.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i,target;
short victim, victimhp;
short randattack;

body;

beginstate INIT_STATE;
	if(get_flag(7,1) == 1) {
		//start the dialog
		force_instant_terrain_redraw();
		set_char_dialogue_pic(ME,527,0); //Trevahn
		set_flag(7,1,2); //this doesn't need to happen again
		begin_talk_mode(10);
		}

break;

beginstate DEAD_STATE;
	//Time for the speech
	reset_dialog();
	add_dialog_str(0,"Trevahn falls to the ground, clutching one of his many wounds. He growls something incoherent.",0);
	add_dialog_str(1,"After taking one long look at you, he pulls a small wooden tube from his belt, and points it at the sky. A bright blue flare shoots out of the end, into the sky.",0);
	add_dialog_str(2,"He slumps over into a pool of his own blood. And with that, Trevahn dies.",0);
	run_dialog(1);
	toggle_quest(2,0);
	
	drop_item(449);
	drop_item(451);

	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state_continue(3);
			else
				set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		set_state_continue(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		set_state_continue(3);
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	//Trevahn has special attacks that trigger when he hits in melee.

	//First, check to see if my target's alive.
	if (target_ok() == FALSE)
		set_state(START_STATE); //If not, go back and get a new one.
	
	//Next, check to see if my target is paralyzed.
	// (attacking a paralyzed person is pointless, deal with next threat)
	// (...also only works if someone hit me)
	if((get_char_status(get_target(), 11) > 0) && (who_hit_me() != -1))
		set_target(ME,who_hit_me());
	
	//Set up variables for the special attack.
	victim = get_target();
	victimhp = get_health(victim);
	
	//Remember to actually attack.
	do_attack();
	
	//If the attack actually did damage to the target, run a special attack.
	// ...of course, only if the victim is still alive.
	if((get_health(victim) < victimhp) && (char_ok(victim) == 1)) {
		//pick one of three special attacks
		randattack = get_ran(1,1,3);
		
		//First option: Paralyzing Fist
		if(randattack == 1) {
			put_effect_on_char(victim,9,1,2);
			run_animation_sound(112); //smite
			print_named_str(victim, " is stunned by Trevahn's attack!");
			set_char_status(victim, 11, 2, 1, 0); //2 levels of paralysis
			}
		
		//Second option: Crippling Blow
		else if(randattack == 2) {
			put_effect_on_char(victim,2,1,2);
			run_animation_sound(65); //draining
			print_named_str(victim, " is weakened by Trevahn's attack!");
			set_char_status(victim, 2, -5, 1, 0); //5 levels of weakening
			}
		
		//Third option: Enfeebling Strike
		else if(randattack == 3) {
			put_effect_on_char(victim,7,1,2);
			run_animation_sound(171); //quick zap
			print_named_str(victim, " is enfeebled by Trevahn's attack!");
			set_char_status(victim, 21, 5, 1, 0); //5 levels of enfeebling
			}
		}
		
	//Now, check to see if my health has dipped below half...
	// if so, time to activate the guards (t7, state 15)
	if((get_flag(7,2) == 0) && (get_health(ME) <= (get_max_health(ME) * 2) / 3)) {
		set_flag(7,2,1);
		run_town_script(15);
		}
	
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;